package com.app.micaihu.view.home.original.fragment

import android.content.Intent
import com.app.base.BaseMvpFragment
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.ad.AdConfig
import com.app.micaihu.bean.news.VideoList
import com.app.micaihu.configure.Config
import com.app.micaihu.configure.LoadType
import com.app.micaihu.configure.TypeConstant
import com.app.micaihu.configure.UmEventId
import com.app.micaihu.utils.MobclickAgentUtils
import com.app.micaihu.view.home.original.adapter.OriginalListAdapter
import com.app.micaihu.view.home.original.contract.OriginalContract
import com.app.micaihu.view.home.original.presenter.OriginalPresenter
import com.app.micaihu.view.main.MainActivity
import com.app.micaihu.view.main.view.WrapContentLinearLayoutManager
import com.app.micaihu.view.newsdetail.VideoDetailActivity
import com.baidu.mobstat.StatService
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.view_common_rv.*

/**
 * 视频
 */
class OriginalVideoListFragment : BaseMvpFragment<OriginalContract.View, OriginalPresenter>(), OriginalContract.View {

    override fun getPresenter() = OriginalPresenter()

    override fun getLayoutId() = R.layout.view_common_rv

    //操作监听
    private var mOnRefreshingListener: MainActivity.OnRefreshingListener? = null
    private val videoListAdapter: OriginalListAdapter by lazy {
        val videoListAdapter = OriginalListAdapter()
        rvList.layoutManager = WrapContentLinearLayoutManager(context)
        rvList.adapter = videoListAdapter

        videoListAdapter.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        videoListAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
        videoListAdapter
    }

    override fun setData() {

    }

    override fun lazyLoad() {
        videoListAdapter.setEmptyView(loadingView)
        mPresenter?.refresh()
    }

    override fun addListeners() {
        refreshLayout.setOnRefreshListener {
            // 这里的作用是防止下拉刷新的时候还可以上拉加载
            videoListAdapter.loadMoreModule.isEnableLoadMore = false
            mPresenter?.refresh()
        }
        videoListAdapter.loadMoreModule.setOnLoadMoreListener {
            mPresenter?.loadMore()
        }
        videoListAdapter.setOnItemClickListener { adapter, view, position ->
            MobclickAgentUtils.event(UmEventId.videolist_click, UmEventId.video)
            val news = videoListAdapter.getItem(position)
            val intent = Intent(activity, VideoDetailActivity::class.java)
            intent.putExtra(Config.Extra.PARAMETER1, news.articleId)
            intent.putExtra(Config.Extra.PARAMETER2, TypeConstant.SOURCETYPE_NORMAL)
            startActivity(intent)
            StatService.onEvent(activity, "055", "原创视频", 1) // 统计的点击的次数
        }
    }

    override fun handleVideoList(video: BaseBean<VideoList?>?, loadType: Int) {
        refreshLayout.finishRefresh(Config.delayRefresh)
        videoListAdapter.loadMoreModule.isEnableLoadMore = true
        mOnRefreshingListener?.let {
            if (LoadType.TYPE_REFRESH_SUCCESS == loadType || LoadType.TYPE_REFRESH_ERROR == loadType) {
                it.onDataRefreshingEnd()
            }
        }
        if (video == null) {
            if (videoListAdapter.data.isNotEmpty()) {
                if (LoadType.TYPE_REFRESH_ERROR == loadType) {
                    ToastUtils.showShort(R.string.empty_network_error)
                } else {
                    videoListAdapter.loadMoreModule.loadMoreFail()
                }
            } else {
                if (LoadType.TYPE_REFRESH_ERROR == loadType) {
                    videoListAdapter.setEmptyView(errorView)
                } else {
                    videoListAdapter.setEmptyView(getEmptyView())
                }
            }
        } else {
            if (video.isSuccess()) {
                video.data?.let {
                    if (LoadType.TYPE_REFRESH_SUCCESS == loadType) {
                        val list = it.newsList
                        LogUtils.e("video","1 video ->"+list.size)
                        AdConfig.insertAd(list, it.adList, 0, 0, null)
                        LogUtils.e("video","2 video ->"+list.size)
                        videoListAdapter.setList(list)
                    } else if (LoadType.TYPE_LOAD_MORE_SUCCESS == loadType) {
                        val list = videoListAdapter.data
                        LogUtils.e("video","load video ->"+list.size)
                        AdConfig.insertAd(list, it.adList, 0, 0, null)
                        LogUtils.e("video","load video ->"+list.size)
                        videoListAdapter.addData(list)
                    }
                    if (it.newsList.isNullOrEmpty()) {
                        videoListAdapter.setEmptyView(getEmptyView())
                        videoListAdapter.loadMoreModule.loadMoreEnd()
                    } else {
                        videoListAdapter.loadMoreModule.loadMoreComplete()
                    }
                }
            } else {
                ToastUtils.showShort(video.msg)
            }
        }
    }

    /* 供外部调用刷新数据 */
    fun flushData() {
        rvList.smoothScrollToPosition(0)
        refreshLayout.autoRefresh()
    }

    fun setOnRefreshingListener(onRefreshingListener: MainActivity.OnRefreshingListener) {
        this.mOnRefreshingListener = onRefreshingListener
    }


}